package com.jwpdk.appframework.controller;


import java.io.UnsupportedEncodingException;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.log4j.Logger;

import com.jwpdk.util.StringUtils;

/**
 * * 此类暂时保留但暂时不用
 * 配置为utf-8+tomcat6默认测试通过
 * @author 王志伟
 *
 */
public class JwpdkRequest extends HttpServletRequestWrapper {
	private final Logger log	= Logger.getLogger(this.getClass());
	//private String	encode;
	
	public JwpdkRequest(ServletRequest request) {
		super((HttpServletRequest)request);
		//this.encode = encode;
	}

	private HttpServletRequest getHttpServletRequest() {
		return (HttpServletRequest) super.getRequest();
	}

	/**
	 * 转换字符串编码
	 * @param para
	 * @return
	 */
	private String transTo(String para) {
		if(para==null) return null;
		String encode1 = StringUtils.getEncode(para);
		String encode2;
		String _para = null;
		try {
			encode2 = StringUtils.getEncode(new String(para.getBytes(encode1)));
			_para = new String(para.getBytes(encode1), encode2);
		} catch ( UnsupportedEncodingException e ) {
			log.error(e);
			e.printStackTrace();
		}
		return _para;
	}
	/*
	private String transTo(String para) {
		try {
			System.out.println("testencode:"+getEncoding(para));
			String _encode = StringUtils.getEncode(para,encode);
			byte[] bytes = para.getBytes("8859_1");
			return new String(bytes, _encode);
		} catch ( Exception ex ) {
		}
		return null;
	}
	*/
	
	@Override
	public String getParameter(String name) {
		return transTo(getHttpServletRequest().getParameter(name));
	}

	@Override
	public String[] getParameterValues(String name) {
		String values[] = getHttpServletRequest().getParameterValues(name);
		if ( values != null ) {
			for (int i = 0; i < values.length; i++) {
				values[i] = transTo(values[i]);
			}
		}
		return values;
	}
}
